import 'package:flutter/material.dart';
import 'package:get/instance_manager.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/rules.dart';
import 'package:yp/config/style.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:yp/utils/util.dart';
import 'package:yp/widgets/index.dart';
import 'package:get/get.dart';

class ExpertUnderReview extends StatefulWidget {
  const ExpertUnderReview({Key key}) : super(key: key);

  @override
  _ExpertUnderReviewState createState() => _ExpertUnderReviewState();
}

class _ExpertUnderReviewState extends State<ExpertUnderReview> {
  bool isShow = false;
  var begTime = '';
  var overTime = '';
  var status_num_id = 1;
  Map distribution_salesperson_apply;

  //查询申请
  getapplysubmit() {
    var params = {
      'leader_usr_num_id': SpUtil.getInt('empeNumId'),
      "access_token": SpUtil.getString('token'),
    };

    Api.applyquery(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          distribution_salesperson_apply = event['distribution_salesperson_apply'];
          begTime = getyearDayFromTimestamp(distribution_salesperson_apply['apply_dtme']);
          status_num_id = distribution_salesperson_apply['status_num_id'];
          if (status_num_id != 1) {
            overTime = getyearDayFromTimestamp(distribution_salesperson_apply['review_dtme']);
          }

          print(begTime);
          print(overTime);
          if (distribution_salesperson_apply != null) {
            isShow = true;
          }
        });
      }
    });
  }

  void initState() {
    super.initState();
    getapplysubmit();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: YPNavigationBar('申请成为慧达人'),
      body: Visibility(
        visible: isShow,
        child: Column(children: [_header(), SizedBox(height: 22), _succbottom(context), _bottom(context)]),
      ),
    );
  }

  Widget _header() {
    if (isShow) {
      return Container(
          color: EsColors.White,
          padding: EdgeInsets.symmetric(vertical: 16, horizontal: 25),
          child: Row(children: [
            Column(children: [Image.asset('assets/images/audit_submit.png', width: 25, height: 25), SizedBox(height: 8), Text('提交申请', style: TextStyle(color: EsColors.Title, fontSize: EsSizes.SM, fontWeight: EsFontWeights.Normal)), SizedBox(height: 2), Text(begTime, style: TextStyle(color: EsColors.ContentSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Normal))]),
            SizedBox(width: 14),
            Expanded(
                child: Padding(
              padding: const EdgeInsets.only(top: 12),
              child: YpLine(color: Color(0xffe8e8e8)),
            )),
            SizedBox(width: 14),
            Column(children: [rules('a14', distribution_salesperson_apply) ? Image.asset('assets/images/audit_wait.png', width: 25, height: 25) : Image.asset('assets/images/audit_submit.png', width: 25, height: 25), SizedBox(height: 8), Text(rules('a14', distribution_salesperson_apply) ? '等待审核' : '审核完成', style: TextStyle(color: rules('a14', distribution_salesperson_apply) ? EsColors.Primary : EsColors.Title, fontSize: EsSizes.SM, fontWeight: EsFontWeights.Normal)), SizedBox(height: 2), Text(rules('a14', distribution_salesperson_apply) ? '审核中' : overTime, style: TextStyle(color: EsColors.ContentSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Normal))]),
            SizedBox(width: 14),
            Expanded(
                child: Padding(
              padding: const EdgeInsets.only(top: 12),
              child: YpLine(color: Color(0xffe8e8e8)),
            )),
            SizedBox(width: 14),
            Column(children: [
              Image.asset(rules('a14', distribution_salesperson_apply) ? 'assets/images/save.png' : 'assets/images/audit_submit.png', width: 25, height: 25),
              SizedBox(height: 8),
              Text(
                  rules('a14', distribution_salesperson_apply)
                      ? '申请成功'
                      : rules('a15', distribution_salesperson_apply)
                          ? '已同意'
                          : '已拒绝',
                  style: TextStyle(color: status_num_id == 1 ? EsColors.ContentSecondary : EsColors.Title, fontSize: EsSizes.SM, fontWeight: EsFontWeights.Normal)),
              SizedBox(height: 2),
              Text('')
            ])
          ]));
    } else {
      return Container();
    }
  }

  Widget _succbottom(BuildContext context) {
    if (isShow) {
      return Visibility(
        visible: rules('a15', distribution_salesperson_apply),
        child: Container(
          color: EsColors.White,
          padding: EdgeInsets.all(26),
          child: Column(children: [
            Text('重要提醒', style: TextStyle(color: EsColors.Primary, fontSize: EsSizes.LG, fontWeight: EsFontWeights.Medium)),
            SizedBox(height: 14),
            Text('感谢您申请壹品慧生活KOC , 您的申请已通过，请扫描下方二维码下载 “壹品慧” 专属APP，享受自购省钱，分享赚钱的专属特权 !', style: TextStyle(color: EsColors.Title, fontSize: EsSizes.SM, fontWeight: EsFontWeights.Medium)),
            SizedBox(height: 20),
            Image.asset('assets/images/yp_code.png', width: 146, height: 146),
            SizedBox(height: 26),
          ]),
        ),
      );
    } else {
      return Container();
    }
  }

  Widget _bottom(BuildContext context) {
    return TextButton(
        onPressed: () {
          Get.offAllNamed(RouteConfig.main);
        },
        child: Text(
          '返回首页',
          style: TextStyle(color: EsColors.White, fontSize: EsSizes.LG, fontWeight: EsFontWeights.Normal),
        ),
        style: OutlinedButton.styleFrom(minimumSize: Size(MediaQuery.of(context).size.width - 70, 44), primary: EsColors.Primary, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(23)), backgroundColor: EsColors.Primary));
  }
}
